const app = getApp();
const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */ 
  data: {
    orderNum: '',//订单号
    infoList: [
      { "orderNum": '1105 7081 7175 8380', "themeTitle": "一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十", "contentType": 1, "deliveryType": '', 'goodsData': [{ "price": 39, 'designatedType': 0, "sum": 1, "labelName": '' }], "postscript":'这是添加后的备注，限五十个字符！（费用周期不限是指一次购买后可多次使用，直至圈子活动结束）'},

      { "orderNum": '1105 7081 7175 8381', "themeTitle": "速记阅读练习手册有大中小三种规格供选择优惠中（限三十个字符）", "contentType": 4, "deliveryType": 1, "deliveryMoney": '19', 'deliveryTime': '2019.09.01', 'consigneeName': '李霖沣', 'consigneeSex': 1, 'consigneephone': "18670270910", "consigneeAddr": "东莞市南城区鸿福新中路289号天利中央花园5栋一单元办公楼906室", 'goodsData': [{ "price": 39, 'designatedType': 1, "sum": 1, "labelName": '中码' }, { "price": 100, 'designatedType': 1, "sum": 2, "labelName": '大码' }], "postscript": '这是添加后的备注，限五十个字符！（费用周期不限是指一次购买后可多次使用，直至圈子活动结束）', 'deliveryState': 0, "returnState": 0,"isReturn":0}
    ],// list数据
    infoObj: {"orderType":2, 'dealTime': '2019.08.27', 'totalMoney': 9999,'discounts':20,"postscript":'这是付款时的备注，限五十个字符！（费用周期不限是指一次购买后可多次使用，直至圈子活动结束）',"title":"阅读的方式"},// 其他数据
    openid: "",// 当前的用户id
    isSubmit: false,// 是否提交中
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var orderNum = options.orderNum;
    _this.setData({
      //orderNum: orderNum,
      openid: app.globalData.logininfo.openid
    })
    //_this.getOrderDetailByOrderNum();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    var shareFrom = e.from;
    var openid = app.globalData.logininfo.openid;
    var nickName = app.globalData.logininfo.userInfo.nickName;
    if(shareFrom == "button"){
      //通过按钮触发分享
      var type = e.target.dataset.type;
      if(type == 1){
        // 票种转赠好友
        var id = e.target.dataset.id;
        var circleid = e.target.dataset.circleid;
        return {
          title: nickName + "赠送您一张票种",
          path: 'pages/home/shareJump/shareJump?id=' + id + "&type=" + util.getShareType("examplesTicket") + "&isCheckLogin=1&circleid=" + circleid + "&shareOpenid=" + openid,
          imageUrl: "https://image.xingqubaike.com:8443/minicard/ucenter/personcenter/background/scene/5.png"
        }
      }
    }
  },

  /**
   * 通过订单号获取订单详情
   */
  getOrderDetailByOrderNum: function(){
    var _this = this;
    var orderNum = _this.data.orderNum;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = openid;
        data.orderNum = orderNum;
        data.access_token = res.access_token;
        util.post(api.Interest_GetOrderDetailByOrderNum, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              infoList: res.result.list,
              infoObj: res.result.json,
            })
          }else{
            util.showToast(res.message, "none");
          }
        });
      });
    }
  },

  /**
   * 通过票种加入圈子
   */
  joinCircleByAmountDetail: function(e){
    var _this = this;
    var openid = app.globalData.logininfo.openid;
    var circleid = e.currentTarget.dataset.circleid;
    var joinDetailId = e.currentTarget.dataset.id;
    if (openid != null && openid != "") {
      _this.setData({
        isSubmit: true
      })
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.interestJoinDetailId = joinDetailId;
        data.openid = openid;
        data.access_token = res.access_token;
        util.post(api.Interest_JoinCircleByAmountDetail, data).then(function (res) {
          if (res.code == 200) {
            util.showToast("加入成功", "success");
            setTimeout(function () {
              _this.getOrderDetailByOrderNum();
              _this.navigateToCircleInfo(circleid);
            }, 1000)
          } else {
            util.showToast(res.message, "none");
          }
          _this.setData({
            isSubmit: false
          })
        });
      });
    }
  },

  /**
   * 进入圈子详情页面
   */
  navigateToCircleInfo: function (circleid) {
    var _this = this;
    var openid = app.globalData.logininfo.openid;
    var data = {};
    data.circleid = circleid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        _this.userIsCircleJoinQuery(api.Home_UserIsCircleJoinApiUrl, data);
      })
    }
  },

  /**
   * 校验用户是否为已加入的圈子：请求
   */
  userIsCircleJoinQuery: function (url, data) {
    var _this = this;
    util.post(url, data).then(function (res) {
      var userIsCircleJoin = false;
      if (res.code == 200) {
        userIsCircleJoin = res.result.userIsCircleJoin;
      }
      if (userIsCircleJoin) {
        // 已加入
        wx.navigateTo({
          url: '../../../../ucenter/pages/ucenter/personcenter/editdetails/circleinteraction/circleinteraction?circleid=' + data.circleid,
        })
      } else {
        util.showToast("加入圈子失败", "none");
      }
    });
  },

  /**
   * 进入圈子详情页
   */
  navigateToCircledetails: function (e) {
    var circleid = e.currentTarget.dataset.circleid;
    wx.navigateTo({
      url: '../../../../ucenter/pages/ucenter/personcenter/editdetails/circledetails/circledetails?circleid=' + circleid,
    })
  },
  /**
   * 进入订单中心
   */
  gotoOrderCenter: function(){
    wx.navigateTo({
      url: '/interest/pages/interest/orderCentre/orderCentre',
    })
  },
})